/**
 * Created by jaliu on 2015/3/15.
 */
var marketingService = require('../../services/backoffice/backOffice-marketingService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

/**
 * 获取小区互助统计
 * @param req
 * @param res
 */
exports.getCommunityQuestionStatistics = function (req, res) {

    var opt = req.body;

    marketingService.getCommunityQuestionStatisticsService(opt, function (err, result) {
        if (err) {
            logger.error('Exception happened when getCommunityQuestionStatistics...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getCommunityQuestionStatistics...!' + err.message));
        } else {
            var data = result[0];
            data['startNum'] = result[1];
            data['startUserNum'] = result[2];
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.getUserListByHobby = function (req, res) {
    var community = req.body.community;
    var hobbyName = req.body.hobbyName;

    restClient.get('http://' + RecommendHost + ':8003?usersbytag=' + hobbyName + '&community=' + community, function (data, response) {
        if (response.statusCode == 200 && data != null && data != 'unknown') {
            var uList = JSON.parse(data).ids;
            res.json(messageUtil.composeSucceededMessage(req, null, uList));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, []));
        }
    }).on('error', function (err) {
        logger.error('getUserListByHobby---' + err.message);
        res.json(messageUtil.composeSucceededMessage(req, null, []));
    });
};

exports.addCommunity = function (req, res) {
    var input = req.body;
    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
        } else {
            var imageURL;

            images.forEach(function (image) {
                if (image != null) {
                    imageURL = image;
                }
            });

            if (imageURL != null)
                input.communityLogo = imageURL;

            marketingService.addCommunityService(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when addCommunity...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, 'Exception happened when addCommunity...!' + err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    })
};

exports.searchCommunityByName = function (req, res) {
    var name = req.params.name;
    var city = req.params.city;

    marketingService.searchCommunityByNameService(name, city, function (err, result) {
        if (err) {
            logger.error('Exception happened when searchCommunityByName...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when searchCommunityByName...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.changeCommunityStatus = function (req, res) {
    var input = req.body;

    marketingService.changeCommunityStatusService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when changeCommunityStatus...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when changeCommunityStatus...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
}

/* db.community_applications列表 */
exports.getCommunityApplications = function (req, res) {
    var input = req.body;

    marketingService.getCommunityApplicationsService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when changeCommunityStatus...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when changeCommunityStatus...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.createUserCsv = function (req, res) {
    var input = req.body;

    marketingService.createUserCsvService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when createUserCsv...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when createUserCsv...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.createUgcCsv = function (req, res) {
    var input = req.body;

    marketingService.createUgcCsvService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when createUgcCsvService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when createUgcCsvService...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getOperatePushStatistics = function (req, res) {
    marketingService.getOperatePushStatisticsService(function (err, result) {
        if (err) {
            logger.error('Exception happened when getOperatePushStatistics...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message))
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.addActivityToFreshNews = function (req, res) {
    var input = req.body;

    marketingService.addActivityToFreshNewsService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when addActivityToFreshNews...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message))
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};